﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace Warehouse_Management
{
    public partial class PhieuGD  
    {
        public string TenKho { get; set; }
        public List<PhieuGDChiTiet> HangCollection { get; set; }


        public static List<PhieuGD> ConverForDisplay(IDataReader reader)
        {
            List<PhieuGD> collection = new List<PhieuGD>();
            while (reader.Read())
            {
                PhieuGD entity = new PhieuGD();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("LoaiPhieu"))) entity.LoaiPhieu = reader.GetString(reader.GetOrdinal("LoaiPhieu"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaPhieu"))) entity.MaPhieu = reader.GetString(reader.GetOrdinal("MaPhieu"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayNhap"))) entity.NgayNhap = reader.GetDateTime(reader.GetOrdinal("NgayNhap"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaKho"))) entity.MaKho = reader.GetString(reader.GetOrdinal("MaKho"));
                if (!reader.IsDBNull(reader.GetOrdinal("TongTien"))) entity.TongTien = reader.GetDecimal(reader.GetOrdinal("TongTien"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetInt32(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenKho"))) entity.TenKho = reader.GetString(reader.GetOrdinal("TenKho"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        public static IDataReader SelectDynamicFull(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_WM_PhieuGD_SelectDynamicFull]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            return db.ExecuteReader(dbCommand);
        }
        public static List<PhieuGD> LoadAll(string whereCondition, string orderByExpression)
        
        {
            IDataReader read = SelectDynamicFull(whereCondition, orderByExpression);
            return ConverForDisplay(read);

        }
        public void TaoMaPhieuMoi(char loaiPhieu,string MaKho)
        {
            string cmd = "select count(*) from [dbo].[t_WM_PhieuGD] where  LoaiPhieu = @LoaiPhieu AND MaKho = @MaKho";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(cmd);
            db.AddInParameter(dbCommand, "@LoaiPhieu", SqlDbType.Char, loaiPhieu);
            db.AddInParameter(dbCommand, "@MaKho", SqlDbType.VarChar, MaKho);
            object soLuong = db.ExecuteScalar(dbCommand);
            this.MaPhieu = loaiPhieu + ((int)soLuong+1).ToString();
            this.LoaiPhieu = loaiPhieu.ToString();
        }
        public void LoadHangCollection()
        {
            this.HangCollection = PhieuGDChiTiet.SelectCollectionBy_Master_ID(this.ID).ToList<PhieuGDChiTiet>();
        }
        public string InsertUpdateFull()
        {
            string valid;
             SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
                          using (SqlConnection connection = (SqlConnection)db.CreateConnection())
                          {
                              connection.Open();
                              using (SqlTransaction transaction = connection.BeginTransaction())
                              {
                      try
                      {
                          if (this.ID > 0)
                          {
                              this.Update(transaction);
                              foreach (PhieuGDChiTiet chitiet in this.HangCollection)
                              {
                                  if (chitiet.ID > 0)
                                      chitiet.Update(transaction);
                                  else
                                  {
                                      chitiet.Master_ID = ID;
                                      chitiet.Insert(transaction);
                                  }
                              }
                          }
                          else
                          {
                              long ID = this.Insert(transaction);
                              foreach (PhieuGDChiTiet chitiet in this.HangCollection)
                              {
                                  if (chitiet.ID > 0)
                                      chitiet.Update(transaction);
                                  else
                                  {
                                      chitiet.Master_ID = ID;
                                      chitiet.Insert(transaction);
                                  }
                              }
                          }
                          
                          
                          transaction.Commit();
                          valid = string.Empty;
                      }
                      catch (Exception ex)
                      {
                          transaction.Rollback();
                          valid = ex.Message;
                      }
                      finally
                      {
                          connection.Close();
                      }
                  }
              }
              return valid;
        }
        public static string DeleteFull(long ID)
        {
            string valid = string.Empty;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        PhieuGD pgd = PhieuGD.Load(ID);
                        pgd.LoadHangCollection();
                        foreach (PhieuGDChiTiet hang in pgd.HangCollection)
                        {
                            hang.Delete(transaction);
                        }
                        pgd.Delete();
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        valid = ex.Message;
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return valid;
        }
    }
}
